package controller;

import dao.FieldDao;
import dao.OrderDao;
import org.apache.ibatis.session.SqlSession;
import pojo.Field;
import pojo.Order;
import utils.MyBatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author HLF
 * @date 2022/6/14 15:58
 */

@WebServlet("/fieldController")
public class FieldController extends BaseController {

    /**
     * 查询场地列表
     */
    public List<Field> list() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);

        List<Field> fields = fieldDao.getAllFields();
        sqlSession.close();
        return fields;
    }

    /**
     * 跳转到场地管理页面
     */
    protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Field> fields = this.list();
        req.setAttribute("fields", fields);

        req.getRequestDispatcher("/fieldManage.jsp").forward(req, resp);
    }


    /**
     * 添加场地
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fname = req.getParameter("fname");
        String status = req.getParameter("status");
        String price = req.getParameter("price");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);

        fieldDao.insertField(new Field(null, fname, Integer.valueOf(status), new BigDecimal(price)));
        sqlSession.commit();

        sqlSession.close();
        this.index(req, resp);
    }

    /**
     * 修改场地
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fid = req.getParameter("fid");
        String fname = req.getParameter("fname");
        String status = req.getParameter("status");
        String price = req.getParameter("price");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);

        fieldDao.updateField(new Field(Integer.valueOf(fid), fname, Integer.valueOf(status), new BigDecimal(price)));

        sqlSession.commit();
        sqlSession.close();
        this.index(req, resp);
    }

    /**
     * 根据uid获取场地
     */
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fid = req.getParameter("fid");
        resp.setContentType("text/html;charset=UTF-8");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);

        Field qfield = fieldDao.getByFid(Integer.valueOf(fid));

        req.setAttribute("qfield", qfield);

        this.index(req, resp);
    }

    /**
     * 删除场地
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fid = req.getParameter("fid");
        resp.setContentType("text/html;charset=UTF-8");

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        FieldDao fieldDao = sqlSession.getMapper(FieldDao.class);
        OrderDao orderDao = sqlSession.getMapper(OrderDao.class);

        //删除之前先查看order中是否含有记录，如果有就禁止删除
        List<Order> allOrdersByFid = orderDao.getAllOrdersByFid(Integer.valueOf(fid));

        PrintWriter writer = resp.getWriter();
        if (allOrdersByFid.size() == 0) {
            fieldDao.deleteByFid(Integer.valueOf(fid));
            sqlSession.commit();
            writer.println("删除成功！");
            writer.flush();
        } else {
            writer.println("删除失败，请先处理相关预约记录！");
            writer.flush();
        }
        sqlSession.close();
        //this.index(req, resp);
    }
}
